package com.yxy.controller;

import com.yxy.base.BusinessException;
import com.yxy.base.Pagingable;
import com.yxy.base.RestResponse;
import com.yxy.dto.TableProductDto;
import com.yxy.dto.TableProductTypeDto;
import com.yxy.service.TableProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;

/**
 * @author huangxinrong
 * 商品管理
 */
@RestController
@Slf4j
@RequestMapping("/tableProduct")//必须要加
public class TableProductController {

  @Autowired
  private TableProductService tableProductService;

  // 获取商品列表
  @Pagingable//添加分页
  @PostMapping("/list")
  public RestResponse getList(@RequestBody TableProductDto pojo) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.getlist(pojo));
    return restResponse;
  }


  @Pagingable//添加分页
  @PostMapping("/appGetList")
  public RestResponse appGetList(@RequestBody TableProductDto pojo) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.appGetList(pojo));
    return restResponse;
  }

  @PostMapping("/getProInventoryList")
  public RestResponse getProInventoryList(@RequestBody TableProductDto pojo) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.getProInventoryList(pojo));
    return restResponse;
  }



  @PostMapping("/selectNameAndInventory")
  public RestResponse getSelectNameAndInventory(@RequestBody TableProductDto pojo) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.getSelectNameAndInventory(pojo));
    return restResponse;
  }

  //修改商品信息
  @PostMapping("/update")
  public RestResponse update(@RequestBody @Valid  TableProductDto pojo) throws BusinessException, ParseException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.modify(pojo));
    return restResponse;
  }

  //新增商品信息
  @PostMapping("/create")
  public RestResponse create(@RequestBody @Valid  TableProductDto pojo) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.save(pojo));
    return restResponse;
  }

  //根据id删除商品类型信息
  @PostMapping("/delete")
  public RestResponse delete(@RequestBody @Valid  TableProductDto pojo) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.delete(pojo.getId().toString()));
    return restResponse;
  }



  //商品数量
  @PostMapping("/selectProductCount")
  public RestResponse selectProductCount(@RequestBody @Valid  TableProductDto pojo) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "成功");
    restResponse.setContent(tableProductService.getlist(pojo));
    return restResponse;
  }
}
